import { useReactive } from "."
import { hasOwnProperty, isEqual, isObject } from "../shared/utils"

const get = createrGetter(),
 set = createrSetter()

function createrGetter () {
  return function Getter (target, key, seceiver) {
    const res = Reflect.get(target, key, seceiver)

    if(isObject(key)) {
      return useReactive(key)
    }

    return res
  }
}

function createrSetter () {
  return function Setter ( target, key, value, seciver ) {
    const oldValue = target[key],
     isKeyExist = hasOwnProperty(target, key),
     res = Reflect.set(target, key, value, seciver)

    if(!isKeyExist) {
      console.log("新增响应式属性", key, value)
    } else if (!isEqual(value, oldValue)) {
      console.log("修改响应式属性", key, value)
    }

    return res
  }
}


const mutationHandler = {
  get,
  set
}

export {
  mutationHandler
}